package com.microcity.cleanjava.mvvm.model.api;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MediatorLiveData;

import com.microcity.cleanjava.mvvm.model.api.service.CustomService;
import com.darksider.basemodule.baseentity.Resource;
import com.microcity.cleanjava.mvvm.model.entry.Result;
import com.darksider.basemodule.common.AppExecutors;

import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class UserRepository {
    private final CustomService customService;
    private final AppExecutors appExecutors;

    @Inject
    UserRepository(AppExecutors appExecutors, CustomService customService) {
        this.customService = customService;
        this.appExecutors = appExecutors;
    }

//    public ObservableField<Result> getUser(Context context, String tel, String verCode) {
//        final ObservableField<Result> data = new ObservableField<>();
//
//        newRxService.getLoginInfoRx(tel,verCode)
//                .observeOn(AndroidSchedulers.mainThread())
//                .unsubscribeOn(Schedulers.io())
//                .subscribe(new RxSubscriber<>(context,result -> {
//
//                }));
//
//        return data;
//    }

    //直接从网络获取
    public LiveData<Resource<Result>> loadUser(String tel, String verCode) {

        final MediatorLiveData<Resource<Result>> data = new MediatorLiveData<>();

        data.setValue(null);

        data.addSource(customService.getQuickLoginInfo(tel, verCode), resultApiResponse -> {
            if (resultApiResponse != null) {
                if (resultApiResponse.isSuccessful()) {
                    data.setValue(Resource.success(resultApiResponse.body));
//                    if (null == resultApiResponse.body) {
//                        data.setValue(Resource.empty()); //是否为空 策略不同 不需要展示空视图不用判断是否为空
//                    } else {
//                        data.setValue(Resource.success(resultApiResponse.body));
//                    }
                } else {
                    data.setValue(Resource.error(resultApiResponse.errorMessage, resultApiResponse.body));
                }
            }
        });

        return data;
    }
    //需要数据库存储则使用该方法
//    public LiveData<Resource<Result>> loadUserFromDBWithNet(String tel, String verCode) {
//        return new NetworkBoundResource<Result, Result>(appExecutors) {
//            @Override
//            protected void saveCallResult(@NonNull Result item) {
//                //把数据存入数据库
//            }
//
//            @Override
//            protected boolean shouldFetch(@Nullable Result data) {//判断什么时候需要拉取数据
//                return data == null;
//            }
//
//            @NonNull
//            @Override
//            protected LiveData<Result> loadFromDb() {//读取数据库数据
//                return AbsentLiveData.create();
//            }
//
//            @NonNull
//            @Override
//            protected LiveData<ApiResponse<Result>> createCall() {
//                return customService.getQuickLoginInfo(tel, verCode);
//            }
//        }.asLiveData();
//    }
}